<?php
namespace App\Services\Article\Article;

use App\Models\Article\Article;
use App\Models\Article\ArticleClassify;
use App\Services\Log\Log\LogLogic;

/**
 * 文章微应用逻辑层
 * Class ArticleLogic
 * @package App\Services\Article\Article
 */
class ArticleLogic
{
    /**
     * 文章添加
     * @return array
     */
    public function add($data)
    {
        $data['status'] = 1;
        $data['created_at'] = date('Y-m-d H:i:s', time());
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Article::insertGetId($data);

        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 1;
            $logData['record'] = "文章";
            (new LogLogic())->addLog($logData);
        }

        return auto_return(['id' => $rst], '添加成功！', '文章添加失败！');
    }

    /**
     * 文章编辑
     */
    public function edit($data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Article::where('id', $data['id'])->update($data);

        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "文章";
            (new LogLogic())->addLog($logData);
        }

        return auto_return($rst, '文章编辑成功！', '文章编辑异常');
    }

    /**
     * 删除某篇文章
     * @param $data
     * @return array
     */
    public function del($data)
    {
        $updateData['status'] = 0;
        $updateData['updated_at'] = date('Y-m-d H:i:s', time());
        $rst = Article::where('id', $data['id'])->update($updateData);

        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 3;
            $logData['record'] = "文章";
            (new LogLogic())->addLog($logData);
        }

        return auto_return($rst, '文章已删除！', '文章删除失败!');
    }

    /**
     * 文章列表
     * @param $data
     * @return array
     */
    public function list($data)
    {
        $query = Article::where('status', '=', $data['status']);

        if (isset($data['classify_id']) && $data['classify_id'] != '') {
            $query->where('classify_id', '=', $data['classify_id']);
        }
        if (isset($data['keywords']) && $data['keywords'] != '') {
            $query->where('title', 'like', $data['keywords']);
            $query->where('content', 'like', $data['keywords']);
        }
        if (isset($data['belong_to']) && $data['belong_to'] != '') {
            $query->where('belong_to', '=', $data['belong_to']);
        }

        $list = $query->orderBy('id', 'desc')->paginate(10);
        return auto_return($list, '成功获取文章列表', '获取文章列表失败');
    }

    /**
     * 获取文章信息 非删除
     */
    public function info($data): array
    {
        $_where['status'] = 1;
        $_where['release_status'] = 1;
        $_where['id'] = $data['id'];
        $res = Article::where($_where)->select()->first();
        return auto_return($res, "查询成功！", "查询文章不存在");
    }

    /**
     * 智慧头条下的文章分类和分类下的文章列表 只展示三个 非删除
     */
    public function witList($data): array
    {
        $_where['article.status'] = 1;
        $_where['article_classify.status'] = 1;
        $_where['article.belong_to'] = 1;
        $re = Article::join('article_classify', 'article.classify_id', '=', 'article_classify.id')
            ->select('article.*','article_classify.name as class_name')
            ->where('article_classify.pid', '=', $data['pid'])
            ->orderBy('article_classify.name')
            ->get()
            ->toArray();
        $res = [];
        $c = 0;
        $d = 0;
        $e = 0;
        foreach ($re as $k => $v ) {
                if ($v['class_name'] == '公司动态') {
                    if($c < 2){
                        $res['gsdt'][$c] = $re[$k];
                        $c++;
                    }
                }
                if ($v['class_name'] == '媒体报道') {
                    if($d < 2){
                        $res['mtbd'][$d] = $re[$k];
                        $d++;
                    }
                }
                if ($v['class_name'] == '行业新闻') {
                    if($e < 2){
                        $res['hyxw'][$e] = $re[$k];
                        $e++;
                    }
                }
        }
        return auto_return($res, "查询成功！", "查询文章不存在");
    }
}
